package transport;

import network.TPA;

public class ListeningConnection {
	byte inPort;
	ControlConnection control;
	TPA ownTPA;
	/**
	 * Creates a listening connection. The listener will wait until a SYN has been sent and will then attempt to create a fully developed connection.
	 * @param inPort the port of this application
	 * @param ownTPA the TPA of this transportlayer
	 * @param control the controller of this connection. Manages the connections.
	 */
	public ListeningConnection(byte inPort, TPA ownTPA, ControlConnection control){
		this.control = control;
		this.inPort = inPort;
		this.ownTPA = ownTPA;
	}
	
	public boolean isListening(byte port){
		return inPort == port;
	}
	/**
	 * Creates a connection with the given ports and TPA's. The connection will be used to identify the application to which data should be sent. 
	 * @param outPort the port on the other side
	 * @param externTPA the TPA on the other side
	 */
	public void makeConnection(byte outPort, TPA externTPA){
		TransportConnection connection = new TransportConnection(ownTPA, externTPA, inPort, outPort, control.getTransport(), control);
		control.setConnection(connection);
		control.getTransport().getTable().add(connection);
		control.connected();
	}
	public ControlConnection getControl(){
		return this.control;
	}
	
	public void setPort(byte port){
		inPort = port;
	}
	
	public byte getPort(){
		return inPort;
	}

}
